<?php

namespace app\common\model\dynamic;

use think\Model;


class Dynamic extends Model
{





    // 表名
    protected $name = 'dynamic';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'images_array',
        'createtime_text',
        'zan_count',
        'review_count',
    ];
    public function getZanCountAttr($value, $data)
    {

        return Zan::where('d_id', $data['id'])->count();
    }
    public function getReviewCountAttr($value, $data)
    {
        return Review::where('d_id', $data['id'])->count();
    }
    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i", $value) : $value;
    }
    public function getImagesArrayAttr($value, $data)
    {
        if ($data['images']) {
            $array = explode(',', $data['images']);
            foreach ($array as &$v) {
                $v = cdnurl($v, true);
            }
            return $array;
        }
        return [];
    }

    public function zan()
    {
        return $this->hasMany('app\common\model\dynamic\Zan', 'd_id', 'id');
    }

    public function review()
    {
        return $this->hasMany('app\common\model\dynamic\Review', 'd_id', 'id');
    }

    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
